Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Add the DataController Methods

The DataController has three methods:

  • The addNewEmp() method takes a src object that has fName, lName, and location properties and generates a temporary instance of the Employee class from it. The temporary object is added to a dest dataProvider. Because the src and dest are passed as parameters, the method can handle copying data for both the button click and drag and drop operations.

    Note: Creating the temporary object rather than adding the src object directly is necessary because objects are passed by reference, and therefore any later changes to the src object would be automatically reflected in the dest dataProvider.

  • The doDragEnter() method handles the event of a dragged object passing over an object. On this event you define whether the object can accept the the dragged object using the event.handled property (the default is "false") and if so, what DragManager operation should be carried out if the object is dropped (the default is NONE)
  • The doDragDrop() method handles the event of a dragged object being dropped. The logic is facilitated by properties and methods of the event object that Flex automatically generates and passes to handlers:
    • event.dragSource.dataForFormat("items") automatically returns an array containing the dragged data objects
    • event.target returns the object capable of recieving the data in the location where the drop event occurs (in the case of a DataGrid, this property will be the grid's dataProvider)
  1. After the DataController constructor, add a public method called addNewEmp() that takes two parameters called src and dest (both type Object) and returns nothing:

    public function addNewEmp(src:Object,dest:Object):Void	
    {	
    }
    
  2. Inside the method block, declare a variable called tmpObj of type Employee, and initialize it as a new Employee object:

    var tmpObj:Employee=new Employee();
    
  3. Invoke the Employee addEmpData() method on tmpObj, passing the fName, lName, and location properties of src as arguments:

    tmpObj.addEmpData(src.fName,src.lName,src.location);
    
  4. Invoke the dataProvider addItem() method on dest to add tmpObj:

    dest.addItem(tmpObj);
    
  5. Add another method called doDragEnter() that takes a parameter called event; inside the method block set event.handled=true and event.action=DragManager.COPY:

    public function doDragEnter(event):Void	
    {		
    	event.handled=true;
    	event.action=DragManager.COPY;	
    }
    
  6. Add one more method called doDragDrop() that also takes an event parameter; inside the method block set a variable called items equal to event.dragSource.dataForFormat("items") and another variable called dest equal to event.target:

    public function doDragDrop(event):Void
    {		
    	var items = event.dragSource.dataForFormat("items");
    	var dest = event.target;
    }
    
  7. Add one more line to method that calls the addNewEmp() method to copy the dragged data to the target DataGrid, passing the first element of the items array and dest as arguments :

    addNewEmp(items[0],dest); 
    
  8. You're done! Save the file, and then browse data.mxml application to test it. Try adding an new employee, and also dragging employees into the Engineering or Support Group DataGrids.